<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%!
	// 危急值列表
	String list(Input input, Integer uid) {
		Integer page = 1; // 页码
		Integer hospital = input.getInt("hospital", 20); // 每一页条目
		Integer itemCount = input.getInt("item_count", 20); // 每一页条目
		String appType = input.getDesc("app_type", "hospital"); // 版本类型
		String sinceID = input.getDesc("since_id", "0"); // 下拉刷新
		String maxID = input.getDesc("max_id", "0"); // 上拉加载
		String date = input.getDesc("date", "");
		// 查看某天的危急值
		if (!date.equals("")) {
			date = " and concat(FROM_UNIXTIME(n.cTime/1000,'%Y-%m-%d'), '') = '" + date + "'";
		}
		String order = "desc"; // 排序
		String id = "";
		Message msg = new Message(0);
		if (!sinceID.equals("0")) {
			id = " and n.id > " + sinceID;
			order = "asc";
		} 
		if (!maxID.equals("0")) {
			id = " and n.id < " + maxID;
		}
		if ("section".equals(appType)) {
			List<Integer> hospitals = QBuilder.db("basedb").queryHql("select parent from Hospital where hospital=" + hospital );
			if (isEmpty(hospitals)) {
				msg.setErrcodeAndErrmsg(30041, "没有更多危急值数据");
				return msg.toJson();
			}
			hospital = hospitals.get(0);	
			List<Integer> uids = QBuilder.db("basedb").queryHql("select id from User where userName = (select employeeId from User where id=" + uid + " ) and hospital=" + hospital);
			if (isEmpty(uids)) {
				msg.setErrcodeAndErrmsg(30041, "没有更多危急值数据");
				return msg.toJson();
			}
			uid = uids.get(0);
		}
		String hql = "select new map(n.id as id, n.title as title, n.content as content, concat(FROM_UNIXTIME(n.cTime/1000,'%Y-%m-%d %H:%i:%s'), '')  as create_time, nc.isConfirmed as is_confirmed) from  Notification as n, NotificationConfirm as nc where nc.nid=n.id and nc.uid=" + uid + id  + " and n.hospital=" + hospital + " and n.status=" + STATUS_ENABLE + " and nc.status= " + STATUS_ENABLE + date  +  " order by n.cTime desc";
		List<Map> notifictionList = QBuilder.db("basedb").queryHql(hql, itemCount, page);
		if (isEmpty(notifictionList)) {
			msg.setErrcodeAndErrmsg(30041, "没有更多危急值数据");
			return msg.toJson();
		}
		if (order.equals("asc")) { 
			Collections.reverse(notifictionList);
		}
		msg.put("data", notifictionList);
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, list(input, token.getUid()));
%>
